package cn.com.cifi.mars.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.web.action.masterDataToMars.MasterDataToMarsController;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class WebUtils {

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals(WebUtils.getLocalIP())) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        log.error("###### errorMsg: ", e);
                    }
                    if(inet!=null) {
                    	ipAddress = inet.getHostAddress();
                    }
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress="";
        }
 
        return ipAddress;
    }
	
	 
	 
	
	/**
	 * 获取客户端来源
	 * @param request
	 * @return
	 */
	public static String getSource(HttpServletRequest request) {
		String s1 = request.getHeader("user-agent");
		String source="";
		if(s1.contains("Android")) {
			source="Android移动客户端";
		} else if(s1.contains("iPhone")) {
			source="iPhone移动客户端";
		}  else if(s1.contains("iPad")) {
			source="iPad客户端";
		}else {
			source="pc端";
		}
		return source;
	}
	
	/**
	 * 获取请求路径
	 * @param request
	 * @return
	 */
	public static String getUrl(HttpServletRequest request) {
		String url=request.getRequestURL().toString();
		return url;
	}
	
	/**
	 * 获取本地主机IP地址
	 * @return
	 */
	public static String getLocalIP() {
		// 创建本地主机IP地址对象
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String hostAddr = addr.getHostAddress(); // 获取IP地址
			return hostAddr;
		} catch (UnknownHostException e) {
			log.error("###### errorMsg: ", e);
		}
		return "";
	}

	/**
	 * 获取本地机器名
	 * 
	 * @return
	 */
	public static String getHostName() {
		// 创建本地主机IP地址对象
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String hostName = addr.getHostName(); // 获取本地机器名
			return hostName;
		} catch (UnknownHostException e) {
			log.error("###### errorMsg: ", e);
		}
		return "";
	}
}
